/**
 * 构造index主页的脚本
 */
var Event = YAHOO.util.Event, 
	Button = YAHOO.widget.Button,
	Connect = YAHOO.util.Connect, 
	UA = YAHOO.env.ua, 
	JSON = YAHOO.lang.JSON,
	DataSource = YAHOO.util.DataSource,
	DataTable = YAHOO.widget.DataTable,
	Panel = YAHOO.widget.Panel;

var promotionId;
var attachmentList = [];

/**
 * 简化document.getElementById的写法
 * @param {} tagId
 * @return {}
 */
var element = function(tagId) {
	return document.getElementById(tagId);
}


// ===============导航按钮==============================
Event.onContentReady('navButton', function() {
	var oButtonGroup = new YAHOO.widget.ButtonGroup('buttongroup', {
	});
});

Event.onContentReady('navButton', function() {
	Event.addListener('radio1', 'click', showPromotionList);
});

/**
 * 获取任务的基本信息
 * @return
 */
var refreshBase = function() {
	var callback = {
		cache : false,
		success : function(o) {
			var promotion = JSON.parse(o.responseText);
			element('id').innerText = promotion.id;
			element('name').innerText = promotion.name;
		}
	}
	Connect.asyncRequest('GET', '/promotion/' + promotionId + '/base', callback);
};

/**
 * 获取任务的附件清单
 * @return
 */


var refreshAttachment = function() {
	
	
	var callback = {
		cache : false,
		success : function(o){
			var list=[];
			list = JSON.parse(o.responseText);
			var html='';
			for (var i = 0, len = list.length; i < len; ++i) {
				html=html+'<a href="/store/upload/'+list[i].fileName+'">'+list[i].fileName+'</a>&nbsp;&nbsp;&nbsp;';
			}
			element('attachment').innerHTML=html;
			attachmentList=list;
		}
	};
	Connect.asyncRequest('GET', '/promotion/' + promotionId + '/attachmentList', callback);
}

/**
 * 调查问卷
 */

//----------------------展示调查问卷--------------------------------
var refreshQuestionnaire=function(){
	var callback = {
		cache : false,
		success : function(o) {
			//解析数据
			questionList = JSON.parse(o.responseText);
			//展示问卷
			var div=element('questionnaire');
			div.innerHTML='';
			div.style.display='none';
			for (var i = 0, len = questionList.length; i < len; ++i) {
				var q=[];
				q=questionList[i];
				//每个问题一个fieldset
				var fieldSet = document.createElement('fieldset');
				div.appendChild(fieldSet);
				var legend=document.createElement('legend');
				//问题的第一行是提问，后面是选择答案
				legend.innerText=q[0];
				fieldSet.appendChild(legend);
				var ul=document.createElement('ul');
				fieldSet.appendChild(ul);
				for (var j = 1, lenq = q.length; j < lenq; ++j) {
					var li=document.createElement('li');
					ul.appendChild(li);
					li.innerText=q[j];
				}
			}
			var clearDiv=document.createElement('div');
			clearDiv.style.clear='both';
			div.appendChild(clearDiv);
			div.style.display='';

			//填充问卷表单，每个问题用空行隔开，问题的第一行是提问
			var textarea=element('qList');
			var lines='';
			for (var i = 0, len = questionList.length; i < len; ++i) {
				var q=[];
				q=questionList[i];
				for (var j = 0, lenq = q.length; j < lenq; ++j) {
					lines=lines+q[j]+'\r\n';
				}
				lines=lines+'\r\n';
			}
			textarea.innerText=lines;
		}
	}
	Connect.asyncRequest('GET', '/promotion/' + promotionId + '/questionList', callback);	
};

/**
 * 在右侧显示活动清单
 * @param id
 * @return
 */
var showPromotion=function(id){
	element('promotion').style.display='block';
	promotionId=id;
	//alert('打开活动--'+id);
	refreshBase();
	refreshAttachment();
	refreshQuestionnaire();
};

/**
 * 在左侧导航区显示营销活动清单
 * @return
 */
var showPromotionList = function() {	
	var callback = {
		cache : false,
		success : function(o) {
			var list=[];
			alert(o.responseText);
			list = JSON.parse(o.responseText);
			var html='<ul>';
			for (var i = 0, len = list.length; i < len; ++i) {
				html=html+'<li><a href="#" onclick="showPromotion('+list[i].id+')">'+list[i].desc+'</a></li>';
			}
			html=html+'</ul>';
			var promotionListModule;
			if (!promotionListModule) {
				promotionListModule = new YAHOO.widget.Module("promotionListModule", { visible: false });
			}
			
			promotionListModule.setHeader("营销活动");
			promotionListModule.setBody(html);
			promotionListModule.render("left");
			promotionListModule.show();		}
	}
	Connect.asyncRequest('GET', '/promotion/list', callback);
};

showPromotionList();			//测试目的
showPromotion(1);				//为测试


var resultColumnDefs = [
	{key:'custName', label:'客户', sortable:true, resizeable:true},
	{key:'content', label:'反馈', sortable:true, resizeable:true},
	{key:'date', label:'日期', sortable:true, sortOptions:{defaultDir:DataTable.CLASS_DESC},resizeable:true}
];

var refreshResultTable=function(o){
	var ds = new DataSource('/promotion/' + promotionId + '/resultList');
	ds.responseType = DataSource.TYPE_JSARRAY;
	ds.responseSchema = {fields: [{key:'id', parser:'number'},
	                              {key:'custName'},
	                              {key:'content'},
	                              {key:'date'}
	                              ]};
	var table=new DataTable('resultTable', resultColumnDefs, ds, {selectionMode:"single"});
    table.subscribe("rowMouseoverEvent", table.onEventHighlightRow);
    table.subscribe("rowMouseoutEvent", table.onEventUnhighlightRow);
    
    var openResultDialog=function(o) {
    	var target=o.target;
    	var record=this.getRecord(target);
    	alert(record.getData('content'));
    };
    table.subscribe("rowClickEvent", openResultDialog);	
};

refreshResultTable();			//为测试




